package com._public._control._cache 
{
	import com._public._air.WriteFile;
	import com._public._net.LoadData;
	import flash.display.Loader;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	/**
	 * 本地数据缓存
	 * @author WZH(shch8.com)
	 * @example
	 * var cacheLocal:LocalCache = new LocalCache;
		cacheLocal.cachePath = "E:\\cacheTest"//缓存文件名设置
		cacheLocal.saveData("缓存字符","test")
		LoadData.getInstance().loadByteArray("http://sg.game2.cn/images/127961818322.jpg",getFile)
	 */
	public class LocalCache implements ICache
	{
		private static var Instace:LocalCache;
		public static function getInstance():LocalCache {
			if (Instace == null) Instace = new LocalCache;
			return Instace;
		}
		private var FileClass:Object = getDefinitionByName("flash.filesystem.File") as Object;
		
		
		private var _cachePath:String = "";
		
		/**
		 * 是否同步获取数据，数据量较大的缓存建议设为false采用异步获取，监听Event.COMPLETE事件获取数据
		 */
		public var synchroGet:Boolean = true;
				
		/**
		 * 设置本地缓存路径
		 */
		public function set cachePath(value:String):void
		{
			if (value.slice(value.length - 1) != "/" && value.slice(value.length - 1) != "\\") {
				value+="/"
			}
			_cachePath = value;
		}
		public function get cachePath():String
		{
			if (_cachePath == "") {
				trace("LocalCache未设置缓存路径.")
			}
			return _cachePath;
		}
		
		/**
		 * 保存对象
		 * @param	data
		 * @param	id
		 */
		public function saveData(data:Object,id:String):void
		{
			if (cachePath == "") {
				trace("LocalCache未设置缓存路径.")
				return
			}
			if (data is ByteArray) {
				WriteFile.getInstance().writeByte(cachePath+id,data as ByteArray);
			}else {
				WriteFile.getInstance().writeTxt(cachePath+id,String(data));
			}
		}
		
		/**
		 * 清除所有已存储对象
		 */
		public function clearData():void {
			var files:Object=new FileClass(cachePath);
			var ary:Array=files.getDirectoryListing();
			for(var i:Object in ary){
				WriteFile.getInstance().delFile((ary[i].nativePath));
			}
		}
		/**
		 * 同步获取字符型数据，如果获取失败，则为null
		 * @param	id
		 * @return
		 */
		public function getData(id:String):Object {
			var getStr:String = WriteFile.getInstance().readTxt(cachePath+id);
			if (getStr == "") return null;
			return getStr;
		}
		
		/**
		 * 跟据ID删除单个缓存
		 * @param	id
		 */
		public function delData(id:String):void {
			WriteFile.getInstance().delFile(cachePath+id)
		}
		
		/**
		 * 缓存图片加载
		 * @param	id			加载ID
		 * @param	onComplete	加载成功时回调
		 * @param	onError		加载失败时回调
		 * @return
		 */
		public function loadTxt(id:String,onComplete:Function=null,onError:Function=null):URLLoader {
			return LoadData.getInstance().loadXML(cachePath + id , onComplete, null,false,onError);
		}
		
		/**
		 * 缓存图片加载
		 * @param	id			加载ID
		 * @param	onComplete	加载成功时回调
		 * @param	onError		加载失败时回调
		 * @return
		 */
		public function loadImg(id:String,onComplete:Function=null,onError:Function=null):Loader {
			return LoadData.getInstance().loadFile(cachePath + id , onComplete, onError);
		}
		
		/**
		 * 获取文件列表数据，内容为File对象
		 * @return
		 */
		public function getList():Array
		{
			var files:Object=new FileClass(cachePath);
			return files.getDirectoryListing();
		}
	}

}